/**
 * 
 */
package cn.remex.contrib;

import cn.remex.contrib.appbeans.AdminBsCvo;
import cn.remex.contrib.appbeans.AdminBsRvo;
import cn.remex.contrib.appbeans.DataCvo;
import cn.remex.core.exception.ServiceCode;
import cn.remex.core.reflect.ReflectUtil;
import cn.remex.db.Database;
import cn.remex.db.DbCvo;
import cn.remex.db.model.SysMenu;
import cn.remex.db.model.SysConfig;
import cn.remex.db.rsql.model.Modelable;
import cn.remex.db.rsql.model.ModelableImpl;
import cn.remex.db.sql.Sort;
import cn.remex.web.service.BsRvo;

import java.util.ArrayList;
import java.util.List;

import static cn.remex.db.sql.WhereRuleOper.equal;

/** 
 * @author liuhengyang 
 * date 2015-8-29 下午6:22:25
 * @version 版本号码
 */
public class RemexAdminUtil {
	private static SysMenu sysMenu;
	public static BsRvo obtainAdminRvo(AdminBsCvo bsCvo, String rv, String rp) {
		AdminBsRvo rvoBody;

		//if(null==sysMenu){
			SysMenu l0 = new SysMenu();
			SysMenu sysMenu1 = Database.createDbCvo(SysMenu.class)
					.orderBy(SysMenu::getNodeOrder, Sort.ASC)
					.filterBy(SysMenu::getNodeName, equal, "root")
					.ready().query().obtainBean();
			ReflectUtil.copyProperties(l0, sysMenu1);
			
			l0.setSubMenus(new ArrayList<>());
			List<SysMenu> sm1list = Database.createDbCvo(SysMenu.class)
					.orderBy(SysMenu::getNodeOrder, Sort.ASC)
					.filterBy(SysMenu::getSupMenu, equal, sysMenu1.getId())
					.ready().queryBeans();
			if(sm1list.size()>0)
			for(SysMenu sm1:sm1list){
				SysMenu l1 = new SysMenu();
				ReflectUtil.copyProperties(l1, sm1);
				l1.setSupMenu(null);
				l0.getSubMenus().add(l1);
				
				l1.setSubMenus(new ArrayList<>());
				List<SysMenu> sm2list = Database.createDbCvo(SysMenu.class)
						.orderBy(SysMenu::getNodeOrder, Sort.ASC)
						.filterBy(SysMenu::getSupMenu, equal, sm1.getId())
						.ready().queryBeans();
				if(sm2list.size()>0)
				for(SysMenu sm2:sm2list){
					SysMenu l2 = new SysMenu();
					ReflectUtil.copyProperties(l2, sm2);
					l2.setSupMenu(null);
					l1.getSubMenus().add(l2);
				}
			}
			
			sysMenu = l0;
		//}

		AdminBsRvo bsRvo = new AdminBsRvo();
		bsRvo.setCode(ServiceCode.SUCCESS);
		bsRvo.setSysMenus(sysMenu.getSubMenus());
		bsRvo.setResponseHandelParam(rv);
		return bsRvo;
	}
	
	public static <T extends ModelableImpl> DbCvo<T, ?> obtainDbCvo(Class<T> clazz, DataCvo bsCvo){
		DbCvo<T, ?> ret = Database.createDbCvo(clazz)//.orderBy(t->t.getId(),Sort.ASC)
				.rowCount(bsCvo.getRowCount()).page(bsCvo.getPagination());
		if(bsCvo.getFilter()!=null){
			ret.setFilter(bsCvo.getFilter());
		}
		return ret;
	}
//	public static <T extends ModelableImpl> void saveDataMeta(BsRvo bsRvo,DbRvo dbRvo){
//		AdminBsRvo adminBsRvo = (AdminBsRvo) bsRvo.getBody();
//		DataCvo meta = new DataCvo();
//
//		meta.setRowCount(12);
//		meta.setPagination(dbRvo.getPagination());
//		meta.setRecordCount(dbRvo.getRecordCount());
//		if(dbRvo.getRowCount() != 0){
//			double b = (meta.getRecordCount()*1.00)/meta.getRowCount();
//			meta.setPageCount((int)Math.ceil(b));
//		}
//		adminBsRvo.setDataCvo(meta);
//	}
//	private static HashMap<String, String> config = new HashMap<String, String>();
	public static String obtainConfig(String key){
		return obtainConfig("default","default","default","default","default", key);
	}
	public static SysConfig obtainConfigInfo(String key){
		return obtainConfigInfo("default","default","default","default","default", key);
	}
	public static String obtainConfig(String environment,String confClass,String confKind,String confType,String group,String key){
		SysConfig conf = obtainConfigInfo(environment, confClass, confKind, confType, group, key);
		return conf == null ? "" : conf.getValue();
	}
	public static SysConfig obtainConfigInfo(String environment,String confClass,String confKind,String confType,String group,String key){
		return Database.createDbCvo(SysConfig.class)
				.filterBy(SysConfig::getEnvironment, equal, environment)
				.filterBy(SysConfig::getConfClass, equal, confClass)
				.filterBy(SysConfig::getConfKind, equal, confKind)
				.filterBy(SysConfig::getConfType, equal, confType)
				.filterBy(SysConfig::getGroup, equal, group)
				.filterBy(SysConfig::getKey, equal, key)
				.rowCount(1).ready().queryBean();
	}

	public static void saveConfig(String key,String value){
		saveConfig("default","default","default","default","default", key,value);
	}
	public static void saveConfig(String environment,String confClass,String confKind,String confType,String group,String key,String value){
		SysConfig conf = new SysConfig();
		conf.setEnvironment(environment);
		conf.setConfClass(confClass);
		conf.setConfKind(confKind);
		conf.setConfType(confType);
		conf.setGroup(group);
		conf.setKey(key);
		conf.setValue(value);
		Database.createDbCvo(SysConfig.class)
				.withBase(sc->{sc.getEnvironment();sc.getConfClass();sc.getConfKind();sc.getConfType();sc.getKey(); sc.getValue();})
				.ready().store(conf);
	}
}
